package com.example.shellshopping;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.shellshopping.YCB_settings.YCBAboutActivity;
import com.example.shellshopping.login.PersonCenterMainActivity;
import com.example.shellshopping.login.UserBean;
import com.example.shellshopping.personCenter.PersonCenterSettingActivity;
import com.example.shellshopping.utils.SaveUserUtils;
import com.example.shellshopping.utils.URLUtils;

import java.io.File;

/**
 * 我的碎片
 */
public class MeFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    public MeFragment() {
    }

    Button settingBTN,centerBTN,aboutBTN;
    String imgpath;
    ImageView headIv;
    TextView nicknameTv;
    private UserBean.DataBean userInfo;

    public static MeFragment newInstance(String param1, String param2) {
        MeFragment fragment = new MeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        //LayoutInflater inflater = LayoutInflater.from(getContext());
        //View view = inflater.inflate(R.layout.fragment_me, null);

        //initView(view);
        //setListener();
    }

    private void setListener() {
        settingBTN.setOnClickListener(onClickListener);
        centerBTN.setOnClickListener(onClickListener);
        aboutBTN.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.frag_me_btn5:
                    //判断是否登录
                    boolean login1 = isLogin();
                    //根据登录情况进行跳转
                    jumpToWhere1(login1);
                    break;
                case R.id.frag_me_btn3:
                    //判断是否登录
                    boolean login2 = isLogin();
                    //根据登录情况进行跳转
                    jumpToWhere2(login2);
                    break;
                case R.id.frag_me_btn4:
                    //根据登录情况进行跳转
                    jumpToAbout();
                    break;
            }
        }
    };

    private void jumpToAbout() {
        Intent intent = new Intent(getContext(), YCBAboutActivity.class);
        startActivity(intent);
    }

    private void jumpToWhere1(boolean login) {
        Intent intent = new Intent(getContext(), PersonCenterSettingActivity.class);
        if (login) {
            startActivity(intent);
        }else {
            intent = new Intent(getContext(), LoginRegActivity.class);
            intent.putExtra("flag",0);
            startActivity(intent);
        }
    }
    private void jumpToWhere2(boolean login) {
        Intent intent = new Intent(getContext(), PersonCenterMainActivity.class);
        if (login) {
            startActivity(intent);
        }else {
            intent = new Intent(getContext(), LoginRegActivity.class);
            intent.putExtra("flag",0);
            startActivity(intent);
        }
    }

    //判断是否登录
    private boolean isLogin() {
        UserBean.DataBean userInfo = SaveUserUtils.getUserInfo(getContext());
        if(userInfo==null)
            return false;
        return true;
    }

    private void initView(View view) {
        settingBTN = view.findViewById(R.id.frag_me_btn5);
        centerBTN = view.findViewById(R.id.frag_me_btn3);
        aboutBTN = view.findViewById(R.id.frag_me_btn4);
        headIv = view.findViewById(R.id.fra_me_tv);
        nicknameTv = view.findViewById(R.id.fra_me_nickname_tv);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view;
        view = inflater.inflate(R.layout.fragment_me, container, false);
        initView(view);
        setListener();
        userInfo = SaveUserUtils.getUserInfo(getContext());
        //imgpath = URLUtils.getImgPath(getContext(),userInfo.getPhone());
        return view;

    }

    @Override
    public void onResume() {
        super.onResume();
        setHeadView();
    }

    private void setHeadView() {
        userInfo = SaveUserUtils.getUserInfo(getContext());
        if (userInfo!=null) {
            imgpath = URLUtils.getImgPath(getContext(),userInfo.getPhone());
            File file = new File(imgpath);
            if (file.exists()) {
                Bitmap bitmap = BitmapFactory.decodeFile(imgpath);
                headIv.setImageBitmap(bitmap);
            }else{
                headIv.setImageResource(R.mipmap.icon);
            }
        }else{
            headIv.setImageResource(R.mipmap.icon);
        }
        if (userInfo!=null) {
            String nickname = userInfo.getNickname();
            nicknameTv.setText(nickname);
        }else{
            nicknameTv.setText("请先登录");
        }
    }
}